/*******************************************************************************
 * Copyright 2007-2008 Vidar Svansson
 *
 * This file is part of JAdapter.
 *
 * JAdapter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JAdapter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JAdapter.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/

package org.jadapter.func;
/**
 * A pair data structure.
 * 
 * @author Vidar Svansson
 *
 * @param <X> Type of the left object
 * @param <Y> Type of the right object
 * 
 * @since 0.1
 * 
 */
public class Pair<X,Y> {
    
    private X x;
    private Y y;

    /**
     * @deprecated
     */
    public Pair() {}
    
    public Pair(X x, Y y) {
        this.x = x;
        this.y = y;
    }
        
    public <T> Pair<Pair<X,Y>,T> append(T t){
    	return NEW (this, t);
    }
    
    public <T> Pair<T,Pair<X,Y>> prepend(T t){
    	return NEW (t, this);
    }
    
        
    /**
     * 
     * @return the left element of this pair.
     */
    public final X x() { return getX(); }
    
    /**
     * 
     * @return the right element of this pair.
     */
    public final Y y() { return getY(); }

    /**
     * @deprecated
     */
    final void x(X x) { setX(x); }

    /**
     * @deprecated
     */
    final void y(Y y) { setY(y); }
    
    public X getX() {
        return x;
    }

    /**
     * @deprecated Pair should be immutable.
     */
    public void setX(X x) {
        this.x = x;
    }
    public Y getY() {
        return y;
    }

    /**
     * @deprecated Pair should be immutable.
     */
    public void setY(Y y) {
        this.y = y;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((x == null) ? 0 : x.hashCode());
        result = prime * result + ((y == null) ? 0 : y.hashCode());
        return result;
    }

    
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final Pair other = (Pair) obj;
        if (x == null) {
            if (other.x != null)
                return false;
        } else if (!x.equals(other.x))
            return false;
        if (y == null) {
            if (other.y != null)
                return false;
        } else if (!y.equals(other.y))
            return false;
        return true;
    }
    
    public static <X,Y>   Pair<X,Y>  NEW(X x, Y y){
        return new Pair<X,Y>(x, y);
    }
   
    
}
